package com.lanou3g.hw0420.a4;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Scanner;

/**
 * Created by dllo on 18/4/21.
 */
public class Main {
    public static void main(String[] args) {
        Socket socket = new Socket();
        PrintWriter pw = null;
        Scanner scanner = new Scanner(System.in);
        try {
            socket.connect(new InetSocketAddress("127.0.0.1", 2333));
            new ReadThread(socket).start();
            OutputStream os = socket.getOutputStream();
            pw = new PrintWriter(new OutputStreamWriter(os));
            while (true) {
                String line = scanner.nextLine();
                pw.println(line);
                pw.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            scanner.close();
            if (pw != null) {
                pw.close();
            }
        }
    }
}
